//
//  PhotoSelectCell.m
//  testApp
//
//  Created by iMac on 2018/2/9.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "PhotoSelectCell.h"
#import "WDPhotoHandler.h"




@interface PhotoSelectCell()

@property (nonatomic, strong) UIImageView *photoImageView;
@property (nonatomic, strong) UIButton *selectButton;

@end

@implementation PhotoSelectCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupInit];
    }
    return self;
}

- (void)setModel:(AssetModel *)model {
    _model = model;
    [[WDPhotoHandler defaultHandler] thumbnalImage:model.asset size:self.bounds.size completion:^(UIImage *image, NSDictionary *info) {
        self.photoImageView.image = image;
    }];
    self.selectButton.selected = model.selected;
}

- (void)setupInit {
    [self.contentView addSubview:self.photoImageView];
    [self.photoImageView addSubview:self.selectButton];
    [self.photoImageView setUserInteractionEnabled:YES];
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.photoImageView.mas_top);
        make.right.mas_equalTo(self.photoImageView.mas_right);
    }];
}


- (void)selectbuttonClick:(UIButton *)sender {
    if (self.selectedAsset) {
        self.selectedAsset(self.model, sender);
    }
}
- (UIImageView *)photoImageView {
    if (!_photoImageView) {
        _photoImageView = [[UIImageView alloc] init];
    }
    return _photoImageView;
}
- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] init];
        [_selectButton setTitle:@"点击选中" forState:UIControlStateNormal];
        [_selectButton setTitle:@"已选中" forState:UIControlStateSelected];
        [_selectButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [_selectButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [_selectButton addTarget:self action:@selector(selectbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

@end
